local xuansi = fk.CreateSkill {
  name = "th_xuansi",
  max_phase_use_time = 1,
}

xuansi:addEffect("active", {
  name = "th_xuansi",
  anim_type = "drawcard",
  prompt = "#th_xuansi",
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_num = 0,
  target_filter = Util.FalseFunc,
  -- can_use = function(self, player)
  --   return player:usedSkillTimes(xuansi.name, Player.HistoryPhase) < 1
  -- end,
  on_use = function(self, room, effect)
    local player = effect.from
    local cards = room:getNCards(2)
    room:turnOverCardsFromDrawPile(player, cards, xuansi.name, true, room:getAllPlayers(false))
    local cardtable = room:askToYiji(player, {
      cards = cards,
      targets = room:getAlivePlayers(false),
      skill_name = xuansi.name,
      min_num = 2,
      max_num = 2,
      prompt = "#th_xuansi-yiji",
      expand_pile = cards
    })
    room:cleanProcessingArea(cards, xuansi.name)
    local tos = {}
    for to, cids in pairs(cardtable) do
      if to ~= player.id and #cids > 0 then
        table.insert(tos, room:getPlayerById(to))
      end
    end
    if #tos < 1 then return end
    local tosD = room:askToChoosePlayers(player, {
      targets = tos,
      min_num = 1,
      max_num = #tos,
      prompt = "#th_xuansi-choose",
      skill_name = xuansi.name
    })
    if #tosD > 0 then
      for _, to in ipairs(tosD) do
        if to.chained or #room:askToDiscard(to, {
          min_num = 1,
          max_num = 1,
          include_equip = true,
          skill_name = xuansi.name,
          cancelable = true,
          prompt = "#th_xuansi-discard:"..player.id
        }) == 0 then
          for _ = 1, 2 do
            if player.dead or to.dead or to:isNude() then break end
            local card = room:askToChooseCard(player, {
              target = to,
              flag = "he",
              skill_name = xuansi.name
            })
            room:throwCard(card, xuansi.name, to, player)
          end
        else
          to:setChainState(true, {
            who = player,
            reason = xuansi.name
          })
        end
      end
    end
  end
}, { check_skill_limit = true })

Fk:loadTranslationTable{
  ["th_xuansi"] = "悬丝",
  [":th_xuansi"] = "出牌阶段限一次，你可以亮出牌堆顶两张牌并将这些牌分配给任意角色，然后你可以令其中任意名其它角色选择一项：1.其弃置一张牌并横置；2.你依次弃置其两张牌。",

  ["#th_xuansi"] = "悬丝：亮出并分配牌堆顶两张牌，然后你可以令其中任意名其它角色选择被弃牌或横置",

  ["#th_xuansi-yiji"] = "悬丝：你须将这些牌分配给任意角色",
  ["#th_xuansi-choose"] = "悬丝：你可以令任意名其他角色选择弃牌并横置或令你依次弃置其两张牌",
  ["#th_xuansi-discard"] = "悬丝：你需弃置一张牌并横置，否则 %src 依次弃置你两张牌",
}

return xuansi